#define PrynInternal
#define PrynImplementation
#include "platform.h"

PrynResult prynLibraryLoadPlatform (PrynLibrary *library)
{
	PrynStartFunction ();
	PrynResult (*init) (PrynLibrary *self);
	const char *filenamez = 0;
	
	prynAssert (library, NullArgument);
	state = library->pState;
	if (library->pPlatform) prynReturnDone ();
	
	prynCall (prynStringToZ (&library->pFilename, library->pState, &filenamez));
	prynAssert (filenamez, OutOfMemory);
	
	library->pPlatform = LoadLibraryA (filenamez);
	prynAssert (library->pPlatform, Failure);
	
	*(void **) &init = GetProcAddress (library->pPlatform, "prynLibraryInit");
	if (init) result = init (library);
	
	PrynEndFunctionAfter (/* success */,
	// Failure:
		if (library && library->pPlatform)
			(FreeLibrary (library->pPlatform), library->pPlatform = 0);
	,
	// Common:
		if (library && filenamez != library->pFilename.pointer)
			prynFree (library->pState, (void *) filenamez);
	);
}

PrynResult prynLibraryDestroyPlatform (PrynLibrary *library)
{
	PrynStartFunction ();
	HANDLE handle;

	prynAssert (library, NullArgument);
	state = library->pState;
	handle = (HANDLE) library->pPlatform;

	if (handle)
		FreeLibrary (handle);
	library->pPlatform = 0;

	PrynEndFunction ();
}